<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HashRouter 实现原理</title>
    <style>
        #result{
            width: 400px;
            height: 300px;
            border:solid 1px #666;
        }
    </style>
</head>
<body>
    <a href="#/home">Home</a>
    <a href="#/about">About</a>
    <hr>
    <div id="result"></div>

    <script>
        //获取 result 元素
        let result = document.querySelector('#result');

        //hashRouter 实现的原理核心点  hashchange 全局事件
        //触发时机: 当页面 URL 的 hash 值发生变化的时候, 该事件会触发
        window.addEventListener('hashchange', () => {
            //根据 URL 中锚点的内容, 切换显示内容
            let hash = location.hash;

            switch(hash){
                case '#/home':
                    result.innerHTML = 'home home home';  // Home
                    break;
                case '#/about':
                    result.innerHTML = 'about about about';   // About
                    break;
                default: 
                    result.innerHTML = '404 Not Found';
                    break;
            }
            console.log(hash);

        });

    </script>
</body>
</html>